package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.MemberLevel;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.MemberLevelDao;
import com.weipai.springboot.module.mapper.MemberLevelMapper;
import com.weipai.springboot.module.service.MemberLevelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/07 15:33
 * 
 * 会员等级
 */
@Service
public class MemberLevelServiceImpl implements MemberLevelService{

    private static final Logger log = LoggerFactory.getLogger(MemberLevelServiceImpl.class);

    @Autowired
    MemberLevelDao memberLevelDao;
    
    //查询数据
    @Override
    public List<MemberLevel> selectAllByDel(int del, String sort, String way) {
        return memberLevelDao.selectAllByDel(del, sort, way);
    }

    //添加
    @Override
    public Response save(MemberLevel memberLevel) {
        Response response = new Response();
        if(1==memberLevelDao.insert(memberLevel)){
            response.setCode(RetCode.SUCCESS);
        }else {
            log.debug("保存会员等级失败:{}",memberLevel.getMemberLevelName());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //修改
    @Override
    public Response update(MemberLevel memberLevel) {
        Response response = new Response();
        if(1==memberLevelDao.updateByPrimaryKey(memberLevel)){
            response.setCode(RetCode.SUCCESS);
        }else{
            log.debug("修改会员等级失败:{}",memberLevel.getMemberLevelName());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //查询会员等级详情
    @Override
    public Response getMemberLevelById(String id) {
        Response response = new Response();
        MemberLevel memberLevel = memberLevelDao.getMemberLevelById(id);
        if(null!=memberLevel){
            response.setCode(RetCode.SUCCESS);
            response.put("data",memberLevel);
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //删除
    @Override
    public Response deleteById(String id) {
        Response response = new Response();
        if(1==memberLevelDao.updateDel(id)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //更改会员等级的状态
    @Override
    public Response changeStatus(String id, Integer status) {
        Response response = new Response();
        if(1==memberLevelDao.updateStatus(id,status)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //批量删除
    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        if(memberLevelDao.batchDeleteById(id)>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }
}
